/* -----------------------------------------------------------------------------
 * PROJECT    : lwf
 * FILE       : ArithmaticsFractionAction.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Apr 11, 2011  mycodetalks			       Created
 */
package net.lwf.web.struts.action.maths.faction;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lwf.handler.IProcessHandler;
import lwf.handler.entities.RadioData;
import lwf.web.struts.action.constant.IActionConstant;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import net.lwf.entity.FormBasicInfo;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.math.fraction.ArithmaticsFractionHandler;
import net.lwf.jaxb.service.maths.fraction.FractionDataService;
import net.lwf.util.web.WebUtil;
import net.lwf.web.constant.IPageTitle;
import net.lwf.web.constant.IWebConstant;
import net.lwf.web.struts.action.LwfWorksheetAction;

/**
 * Class description goes here
 */
public class ArithmaticsFractionAction extends LwfWorksheetAction {
	
	private static final String OPERATION_TYPE = "operationType";
	private static final String COMPLEXITY = "complexity";
	private static final String OPERATION = "arithmatics";
	
	public ActionForward home(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		this.resetQuizLink(request, true);
		//resetOperation(request);
		FractionActionForm faform = (FractionActionForm) form;
		faform.reset();
		this.resetAttributes(request);
		//setBasicFormInfo(request, "Arithmatic Fractions", "", "");
		FractionDataService fds = new FractionDataService();
		faform.setOperations(this.toRadioData(fds.getOperationTypes(getGrade(), OPERATION)));
		return mapping.findForward("home");
	} //home
	
	public ActionForward generate(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		FractionActionForm faform = (FractionActionForm) form;
		this.setAttributes(request, faform);
		
		faform.setAskQuestion(true);
		
		ArithmaticsFractionHandler handler = (ArithmaticsFractionHandler) getHandler(request);
		QuestionInfo qinfo = handler.generateSingle();
		//Set the question data
		setQuestionData(request, qinfo);
		
		faform.setHtmlQuestion(qinfo.getHtmlQuestion());
		//setBasicFormInfo(request, "Arithmatic Fractions", "", getPdfLink());	
		faform.setQuestionNumber(getQuestionNumber(request));
		
		return mapping.findForward("showquestion");
	} //generate
	
	private void setAttributes(HttpServletRequest request, FractionActionForm faform) {
		String operationType = faform.getOperationType();
		String complexity = faform.getComplexity();
		if(operationType != null) {
			WebUtil.setAttribute(request, OPERATION_TYPE, operationType);
		}
		else {
			operationType = (String) WebUtil.getAttribute(request, OPERATION_TYPE);
		}
		if(complexity != null) {
			WebUtil.setAttribute(request, COMPLEXITY, complexity);
		}
		else {
			complexity = (String) WebUtil.getAttribute(request, COMPLEXITY);
		}
		faform.setOperationType(operationType);
		faform.setComplexity(complexity);
	}
	
	public IProcessHandler getHandler(HttpServletRequest request) {
		String operationType = (String) WebUtil.getAttribute(request, OPERATION_TYPE);
		String complexity = (String) WebUtil.getAttribute(request, COMPLEXITY);
		return new ArithmaticsFractionHandler(getGrade(), OPERATION, operationType, complexity);
	}

	@Override
	public String getPdfLink() {
		return IActionConstant.ARITHMATIC_FRACTIONS_PDF_LINK;
	}

	@Override
	public String getQuizLink() {
		return IActionConstant.ARITHMATIC_FRACTIONS_QUIZ_LINK;
	}
	
	@Override
	public String getPageTitle() {
		return IPageTitle.ARITHMATIC_FRACTIONS;
	}

	@Override
	public String getMainTitle() {
		return IPageTitle.MAIN_FRACTIONS;
	}
	
	private List<RadioData> toRadioData(List<String> data) {
		List<RadioData> radiodata = new ArrayList<RadioData>();
		for(String sdata: data) {
			RadioData radio = new RadioData(sdata, sdata);
			radiodata.add(radio);
		}
		return radiodata;
	}
	
	private void resetAttributes(HttpServletRequest request) {
		WebUtil.removeAttribute(request, OPERATION_TYPE);
		WebUtil.removeAttribute(request, COMPLEXITY);
	}
	
	private void resetQuizLink(HttpServletRequest request, boolean setQuizLinkNull) {
		if(setQuizLinkNull) {
			FormBasicInfo info = (FormBasicInfo) request.getAttribute(IWebConstant.BASIC_FORM_INFO);
			info.setQuizLink("");
			request.setAttribute(IWebConstant.BASIC_FORM_INFO, info);
		}
	}

}



